<?php
/**
 *	KLF Framework
 *
 *	Written by Kristian Oye <kristianoye@gmail.com> on Dec 20, 2011
 *	Copyright (C) 2010-2011
 */
namespace KLF\System\Web\MVC
{
	use KLF\System\KComponent;
	use KLF\System\Collections\KDictionary;
	use KLF\System\Errors\KArgumentException;
	
	/**
	 *  Class KView
	 *
	 *  The base of all views.
	 *
	 *  @package KLF\System\Web\MVC
	 *  @access public
	 *  @author Kristian Oye <kristianoye@gmail.com>
	 *  @since 0.1
	 *
	 *  @method KView NewInstance
	 *  
	 *  @property KDictionary $TempData
	 *  @property IView $View
	 *  @property KDictionary $ViewData
	 *  @property string $ViewName
	 */
	abstract class KView extends KActionResult
	{
		/**
		 * A parent view if one exists.
		 * @var IView $_propParentView
		 */
		protected $_propParentView;
		
		/**
		 * A collection of temporary data.
		 * 
		 * @var KDictionary $_propTempData
		 */
		protected $_propTempData;
		
		/**
		 * The view that will render the output.
		 * 
		 * @var IView $_propView
		 */
		protected $_propView;
		
		/**
		 * Data used to render the view.
		 * 
		 * @var KDictionary $_propViewData
		 */
		protected $_propViewData;
		
		/**
		 * The name of the view.
		 * 
		 * @var string $_propViewName
		 */
		protected $_propViewName;
		
		/**
		 * Constructs a new View object
		 */
		public function __construct()
		{
			$this->_propParentView = null;
			$this->_propTempData = new KDictionary('string', null);
			$this->_propViewData = new KDictionary('string', null);
			$this->_propViewName = '';
			$this->_propView = null;
		}
		
		/**
		 * (non-PHPdoc)
		 * @see KLF\System\Web\MVC.KActionResult::ExecuteResult()
		 */
		public function ExecuteResult(KControllerContext $context)
		{
			if (is_null($context))
			{
				throw new KArgumentException("Argument 1 to ExecuteResult() must be a valid KControllerContext");
			}
			if (is_null($this->_propViewName) || strlen($this->_propViewName) == 0)
			{
				$this->_propViewName = $context->RouteData->GetRequiredString('action');
			}
			if (is_null($this->_propView))
			{
				$this->_propView = $this->GetViewResult($context);
			}
			
			$viewContext = new KViewContext($context, $this->View, $this->ViewData, $this->TempData);
			
			if (!is_null($this->_propView))
			{
				$this->View->Render($viewContext);
			}
		}
		
		/**
		 * Locates a view (if possible)
		 *
		 * @author: Kristian Oye <kristianoye@gmail.com>
		 * @since 0.1
		 * @param unknown_type $viewContext
		 * @return IView A view object
		 */
		public static function FindView(KViewContext $viewContext)
		{
			print "<br/><br/>HELLO WORLD!<br/><br/>\n";
		}
	}
}
